package com.tanhua.server.controller;


import com.itheima.model.mongo.Movement;
import com.itheima.model.mongo.Visitors;
import com.itheima.model.vo.MovementsVo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementContrller {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[]) throws Exception {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    //查询个人状态
    @GetMapping("/all")
    public ResponseEntity<PageResult> myMovement(Long userId,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);

    }

    /**
     * 查询好友动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> friendMovement(@RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pr = movementService.findFriendMovement(page, pageSize);
        return ResponseEntity.ok(pr);

    }

    /**
     * 推荐动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity<PageResult> recommendMovement(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pr = movementService.findRecommendMovements(page, pageSize);
        return ResponseEntity.ok(pr);
    }

    //查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity findOneMovement(@PathVariable("id") String movementId) {

        MovementsVo vo = movementService.findOneMovement(movementId);

        return ResponseEntity.ok(vo);
    }
    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
       Integer likeComment = commentService.likeComment(movementId);
       return ResponseEntity.ok(likeComment);
    }
    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
      Integer dislikeComment = commentService.dislikeComment(movementId);
      return ResponseEntity.ok(dislikeComment);
    }

    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
    /**
     * 查看访问过我的访客
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList () {
       List<VisitorsVo> list = movementService.queryVisitorsList();
       return ResponseEntity.ok(list);
    }
}
